package com.atguigu.gmall.activity.util;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.autoconfigure.cache.CacheProperties;

import java.util.concurrent.TimeUnit;

/**
 * @author: atguigu
 * @create: 2023-01-30 15:34
 */
public class CaffeineConfig {

    public static void main(String[] args) throws InterruptedException {
        Cache<String, String> cache = Caffeine.newBuilder()
                .initialCapacity(0)  //初始化容量
                .maximumSize(3)  //缓存最大数量
                //.expireAfterWrite(1, TimeUnit.DAYS)  //设置有效期
                .build();

        cache.put("1", "1");
        cache.put("2", "2");
        cache.put("3", "3");

        //cache.put("4", "4");
        //清除缓存
        cache.invalidate("1");

        //清理需要时间
        Thread.sleep(100);


        String s = cache.get("1", (defaultVal)->{
            return "atguigu";
        });
        System.out.println(s);
    }
}
